package com.huaxinkaiye.utils;

import com.huaxinkaiye.R;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Created by imac on 15/2/3.
 */
public class ProgressUtil {

    private String tag = "ProgressUtil";
    private Dialog dialog;
    private ImageView spaceshipImage;
    private Context context;
    private TextView tipTextView;

    public ProgressUtil(Context context, String msg) {
        this.context = context;
        this.dialog = createLoadingDialog(context, msg);
    }

    /**
     * 得到自定义的progressDialog
     *
     * @param context
     * @param msg
     * @return
     */
    private Dialog createLoadingDialog(Context context, String msg) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.progress_dialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        // main.xml中的ImageView
        spaceshipImage = (ImageView) v.findViewById(R.id.img);
        tipTextView = (TextView) v.findViewById(R.id.tv_tip);// 提示文字
        // 加载动画
        tipTextView.setText(msg);// 设置加载信息

        Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        return loadingDialog;
    }

    public void setMessage(String message) {
        tipTextView.setText(message);
    }

    public void startLoadingAnimation(Context context) {
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                context, R.anim.load_animation);
        // 使用ImageView显示动画
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);
    }

    public void show() {
        startLoadingAnimation(context);
        if (!dialog.isShowing())
            this.dialog.show();
    }

    public void dismiss() {
        if (this.dialog.isShowing())
            this.dialog.dismiss();
    }


}
